# https://leetcode-cn.com/problems/reshape-the-matrix/
from typing import List


class Solution:
    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
        count = len(mat) * len(mat[0])
        if count != r * c:
            return mat
        col = 0
        row = 0
        ans = [[0] * c for i in range(r)]
        for row_num in mat:
            for col_num in row_num:
                ans[row][col] = col_num
                col += 1
                if col == c:
                    row = row + 1
                    col = 0
        return ans 